
<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
    append-to-body>
    <div slot="title" v-if="!dataForm.id">{{$t('message.add')}}</div>
    <div slot="title" v-if="dataForm.id">{{$t('message.edit')}}</div>
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
      <el-form-item :label="$t('message.dicTitle')" prop="dictLabel" :class="{ 'is-required': !dataForm.dictLabel }">
        <el-input v-model="dataForm.dictLabel" type="text" :placeholder="$t('message.dicTitle')"></el-input>
      </el-form-item>
      <el-form-item :label="$t('message.dicValue')" prop="dictValue" :class="{ 'is-required': !dataForm.dictValue }">
        <el-input v-model="dataForm.dictValue" type="text" :placeholder="$t('message.dicValue')"></el-input>
      </el-form-item>
      <el-form-item :label="$t('message.dicType')" prop="dictType" :class="{ 'is-required': !dataForm.dictType }">
        <el-input v-model="dataForm.dictType" type="text" :placeholder="$t('message.dicType')" readonly="true"></el-input>
      </el-form-item>
       <el-form-item :label="$t('message.sortId')" prop="dictSort" :class="{ 'is-required': !dataForm.dictSort }">
        <el-input v-model="dataForm.dictSort" type="text" :placeholder="$t('message.sortId')"></el-input>
      </el-form-item>
      <el-form-item :label="$t('message.remark')" prop="remark" >
        <el-input type="textarea" v-model="dataForm.remark"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">{{$t('message.cancle')}}</el-button>
      <el-button type="primary" @click="dataFormSubmit()">{{$t('message.submit')}}</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        roleList: [],
        dataForm: {
          dictLabel: '',
          dictValue: '',
          dictType: '',
          dictSort: 0,
          remark: ''
        },
        dataRule: {
          dictLabel: [
            { required: true, message: this.$t('message.noEmpty'), trigger: 'blur' }
          ],
          dictValue: [
            { required: true, message: this.$t('message.noEmpty'), trigger: 'blur' }
          ],
          dictType: [
            { required: true, message: this.$t('message.noEmpty'), trigger: 'blur' }
          ],
          dictSort: [
            { required: true, message: this.$t('message.noEmpty'), trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (dictType, id) {
        this.dataForm.dictType = dictType
        this.dataForm.id = id || 0
        this.visible = true
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(`/sys/dictData/info/${this.dataForm.id}`),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.dataForm.dictLabel = data.dictData.dictLabel
              this.dataForm.dictValue = data.dictData.dictValue
              this.dataForm.dictType = data.dictData.dictType
              this.dataForm.dictSort = data.dictData.dictSort
              this.dataForm.remark = data.dictData.remark
            }
          })
        }
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/sys/dictData/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'dictCode': this.dataForm.id || undefined,
                'dictLabel': this.dataForm.dictLabel,
                'dictValue': this.dataForm.dictValue,
                'dictType': this.dataForm.dictType,
                'dictSort': this.dataForm.dictSort,
                'remark': this.dataForm.remark
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: this.$t('message.success'),
                  type: 'success',
                  duration: 500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(this.$t('message.failure'))
              }
            })
          }
        })
      }
    }
  }
</script>
